/*
  Copyright (C) 2013 Jolla Ltd.
  Contact: Thomas Perl <thomas.perl@jollamobile.com>
  All rights reserved.

  You may use this file under the terms of BSD license as follows:

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Jolla Ltd nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import QtQuick 2.0
import "applist.js" as JS
import Sailfish.Silica 1.0
Page{
    id:showfoodpage
    property int operationType: PageStackAction.Animated
    property int pagenum:1
    property string listid:"2333"
    property bool display:false
    property  string apptype: "hot"

    SilicaFlickable {
        anchors.fill: parent
        PageHeader {
            id:header
            title: "应用列表"
        }
        // PullDownMenu and PushUpMenu must be declared in SilicaFlickable, SilicaListView or SilicaGridView
        PullDownMenu {
            MenuItem {
                text: qsTr("About")
                onClicked: pageStack.push(Qt.resolvedUrl("AboutPage.qml"))
            }
            MenuItem {
                id:hot
                text: "热门"
                onClicked:{
                    pageStack.replace(Qt.resolvedUrl("AppList.qml"),{
                                          "pagenum":pagenum,
                                          "listid":listid,
                                          "apptype":"hot"
                                      })
                }
            }
            MenuItem {
                id:app
                text: "应用"
                onClicked:{
                    pageStack.replace(Qt.resolvedUrl("AppList.qml"),{
                                          "pagenum":pagenum,
                                          "listid":listid,
                                          "apptype":"app"
                                      })
                }
            }
            MenuItem {
                id:game
                text: "游戏"
                onClicked:{
                    pageStack.replace(Qt.resolvedUrl("AppList.qml"),{
                                          "pagenum":pagenum,
                                          "listid":listid,
                                          "apptype":"game"
                                      })
                }
            }
        }
        Item {
            id:root
            y:100
            width: 540
            height: 960-header.height
            Component.onCompleted: {
                JS.loadfoodlist(pagenum,listid,apptype);
            }

            Row {
                id:progress
                spacing: Theme.paddingLarge
                anchors.horizontalCenter: parent.horizontalCenter

                BusyIndicator {
                    running: true
                    size: BusyIndicatorSize.Large
                    anchors.verticalCenter: parent.verticalCenter
                }
            }
            ListModel {
                id:foodlistModel
            }


            SilicaListView {
                id:view
                PushUpMenu{
                    MenuItem{
                        text:"返回顶部"
                        onClicked: view.scrollToTop()
                    }
                }
                anchors.fill:parent
                model : foodlistModel
                clip: true

                delegate:
                    BackgroundItem{
                    id:showlist
                    height: 190
                    width: 530
                    Image{
                        id:foodpic
                        fillMode: Image.PreserveAspectFit;
                        x:implicitWidth*3/2
                        width:  implicitWidth * 4/3
                        height: implicitHeight * 4/3
                        source: icon
                        anchors {
                            left: parent.left
                        }
                    }
                    Label{
                        id:appnameid
                        x:foodpic.width+20
                        text:appname
                        font.pixelSize: Theme.fontSizeMedium
                        truncationMode: TruncationMode.Fade
                        anchors {
                            //left: foodpic.right
                            right: parent.right
                            //rightMargin: Theme.paddingSmall
                        }
                    }
                    Label{
                        id:summaryid
                        x:foodpic.width+20
                        text:"<font size='1' >"+summary+"</font>"
                        font.pixelSize: Theme.fontSizeExtraSmall*4/3
                        wrapMode: Text.WordWrap
                        maximumLineCount: 3
                        //truncationMode: TruncationMode.Fade
                        verticalAlignment : Text.AlignBottom
                        anchors {
                            top: appnameid.bottom
                            left: foodpic.right
                            right: parent.right
                        }
                    }
                    Label{
                        id:categoryid
                        x:foodpic.width+20
                        text:"<font size='1' >"+category+"</font>"
                        font.pixelSize: Theme.fontSizeExtraSmall * 4 / 3
                        font.italic: true
                        horizontalAlignment: Text.AlignRight
                        anchors {
                            left: foodpic.right
                            bottom:parent.bottom
                        }
                    }

                    Label{
                        id:versionid
                        x:foodpic.width+20
                        text:"<font size='1' > 版本:"+version+"</font>"
                        font.pixelSize: Theme.fontSizeExtraSmall * 4 / 3
                        font.italic: true
                        horizontalAlignment: Text.AlignRight
                        anchors {
                            right:appnameid.right
                            bottom:parent.bottom
                        }
                    }
                    Separator {
                        width:parent.width;
                        color: Theme.highlightColor
                    }

                    onClicked: {
                        pageStack.push(Qt.resolvedUrl("AppDetail.qml"),{
                                           "id":appid,
                                           "author":author
                                       })
                    }
                }


                VerticalScrollDecorator {}
                footer: Component{

                    Item {
                        id: footerComponent
                        anchors { left: parent.left; right: parent.right }
                        height: visible ? Theme.itemSizeMedium : 0
                        visible:display
                        signal clicked()

                        Item {
                            id:footItem
                            width: parent.width
                            height: Theme.itemSizeMedium
                            Button {
                                anchors.centerIn: parent
                                text: "加载更多..."
                                onClicked: {
                                    pagenum = pagenum+1
                                    JS.loadfoodlist(pagenum,listid,apptype);
                                }
                            }
                        }
                    }

                }
            }
        }
    }
    
}
